﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL_V1.App_Code.DBBroker;
using EntityObjects;

namespace BAL.Controller
{
    public class ViewRequisitionHistoryControl
    {
        RequisitionDBBroker reqBroker;
        DepartmentDBBroker deptBroker;
        List<Requisition> req_list;
        List<String> status_list;

        public ViewRequisitionHistoryControl()
        {
            reqBroker = RequisitionDBBroker.getRequisitionDBBroker();
            deptBroker = DepartmentDBBroker.getDepartmentDBBroker();
        }

        public List<String> getStatus()
        {
            req_list = reqBroker.selectAllRequisition();

            if (req_list != null)
            {
                status_list = new List<string>();
                foreach (Requisition req in req_list)
                {
                    if (!(status_list.Contains(req.Status)))
                    {
                        status_list.Add(req.Status);
                    }
                }

                return status_list;
            }
            else
            {
                return null;
            }
        }

        public List<Requisition> getRequisitions(String status, String fromDate, String toDate, String empId)
        {
            req_list = new List<Requisition>();
            if (fromDate != null && !(fromDate.Equals("")) && toDate != null && !(toDate.Equals("")))
            {
                if (status.Equals("All"))
                {
                    //only dates
                    req_list = reqBroker.getRequisitionByDate(Convert.ToDateTime(fromDate), Convert.ToDateTime(toDate), empId);
                }
                else
                {
                    //status and dates
                    req_list = reqBroker.getRequisitionbyStatusAndDate(status, Convert.ToDateTime(fromDate), Convert.ToDateTime(toDate), empId);
                }
            }
            else if (status != null && !(status.Equals("All")))
            {
                //only status
                req_list = reqBroker.getRequisitionByStatus(status, empId);
            }
            else
            {
                req_list = reqBroker.getRequisitionByEmpId(empId);
            }

            if (req_list != null)
            {
                foreach (Requisition r in req_list)
                {
                    String[] dates = (r.RequestedDate).Split(' ');
                    r.RequestedDate = dates[0].ToString();
                }

                return req_list;
            }
            else
            {
                return null;
            }

        }

        public Requisition getRequisitionByReqFormId(int formId)
        {
            Requisition r = reqBroker.selectRequisition(formId);
            if (r != null)
            {
                return r;
            }
            else
            {
                return null;
            }
        }

        public String getDeptName(int reqFormId)
        {
            Requisition r = getRequisitionByReqFormId(reqFormId);
            if (r != null)
            {
                Department dept = deptBroker.selectDepartment(r.DeptId);
                return dept.DeptName;
            }
            else
            {
                return null;
            }
        }

        public List<RequisitionItems> getRequisitionItems(int reqFormId)
        {
            List<RequisitionItems> list = reqBroker.getRequisitionItemsbyReqFormId(reqFormId);
            return list;
        }
    }
}
